package org.phoenixframework.common.tools.function;

import cn.hutool.core.util.ObjectUtil;

/**
 * Created by IntelliJ IDEA.
 *
 * @author gaodun
 * DateTime: 2021/12/24 17:24
 * Description:
 */
public class FunctionUtil {

    public static ConsumerFunction toConsumer(boolean flag) {
        return str -> {
            // 自己的逻辑
            if (flag) {
                System.out.println(str);
            }
        };
    }

    public static SupplierFunction toSupplier() {
        return () -> "123123";
    }

    /**
     * 参数为true或false时，分别进行不同的操作
     *
     * @param b true、false
     * @return {@link RunnableFunction}
     **/
    public static RunnableFunction toRunnable(boolean b) {

        return (trueHandle, falseHandle) -> {
            if (b) {
                trueHandle.run();
            } else {
                falseHandle.run();
            }
        };
    }

    public static ConsumerAndRunnableFunction<?> toConsumerAndRunnable(String str) {
        return (consumer, runnable) -> {
            if (ObjectUtil.isNotEmpty(str)) {
                consumer.accept(str);
            } else {
                runnable.run();
            }
        };
    }

    public static SelfFunction toSelfFunction(Boolean flag) {
        return num -> {
            if (flag) {
                return String.valueOf(num);
            } else {
                return "-1";
            }
        };
    }
}
